package com.itany.boot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.boot.constant.ResponseCodeConstant;
import com.itany.boot.dao.UserMapper;
import com.itany.boot.entity.User;
import com.itany.boot.exception.AdminUserException;
import com.itany.boot.exception.CodeErrorException;
import com.itany.boot.exception.MD5ErrorException;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.AdminUserService;
import com.itany.boot.service.UserService;
import com.itany.boot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
//是 @Controller + @ResponseBody 的组合注解。
@RestController
@RequestMapping("/user")
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;
    @RequestMapping("/login")
    public ModelAndView login(String loginName, String password, String code, HttpSession session){
        ModelAndView mav=new ModelAndView();
        String image = (String) session.getAttribute("code");
        try {
            User user= adminUserService.login(loginName,password,code,image);
            session.setAttribute("user",user);
            mav.setViewName("redirect:/showMain");
        } catch (RequestParameterErrorException e) {
            mav.addObject("userMsg", e.getMessage());
            mav.setViewName("back_login");
        } catch (CodeErrorException e) {
            mav.addObject("userMsg", e.getMessage());
            mav.setViewName("back_login");
        } catch (AdminUserException e) {
            mav.addObject("userMsg", e.getMessage());
            mav.setViewName("back_login");
        }catch (Exception e){
            e.printStackTrace();
            mav.addObject("userMsg", "服务器内部异常");
            mav.setViewName("back_login");
        }


        return mav;
    }
    @RequestMapping("/findByParams")
    public ModelAndView findByParams (User userParam,String pageNo,String pageSize){
        ModelAndView mav=new ModelAndView();
        PageInfo<User> userPage=adminUserService.findByParams(userParam,pageNo,pageSize);
        mav.addObject("userPage",userPage);
        mav.setViewName("back_userSet");
//讲条件参数带到前台
        mav.addObject("userParam",userParam);
        return mav;

    }
    @RequestMapping("/findById")
    public ResponseResult findById(String id){
//        System.out.println("Received request for user ID: " + id); // 添加日志
        ResponseResult result=new ResponseResult();

        try {
            User user=adminUserService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
//            设置响应数据
            result.setReturnObject(user);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @RequestMapping("/modifyUser")
    public ResponseResult modify(String id,String nickname,String role,String password,String email){
        ResponseResult result=new ResponseResult();
        try {
            adminUserService.modifyUser(id,nickname,role,password,email);
            result.setMessage("修改成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id,String status){
        ResponseResult result=new ResponseResult();
        try {
            adminUserService.modifyStatus(id,status);
            result.setMessage("修改成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;

    }


}
